package com.poetry.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.poetry.domain.po.Poem;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;

import java.util.List;

public interface PoemMapper extends BaseMapper<Poem> {
    @Query("SELECT p FROM Poem p JOIN p.categories c WHERE c.id = :categoryId")
    Page<Poem> getByCategory(@Param("categoryId") Integer categoryId, Pageable pageable);

    // 通过多个分类检索（AND关系）
    @Query("SELECT p FROM Poem p " +
            "WHERE p.id IN (" +
            "  SELECT pc.poemId FROM PoemCategory pc " +
            "  WHERE pc.categoryId IN :categoryIds " +
            "  GROUP BY pc.poemId " +
            "  HAVING COUNT(DISTINCT pc.categoryId) = :categoryCount" +
            ")")
    Page<Poem> getByCategories(@Param("categoryIds") List<Integer> categoryIds,
                                @Param("categoryCount") Long categoryCount,
                                Pageable pageable);
}
